---! 正在战斗的队列 多个战斗
---! key:combat_id value:combat_ob
local new_combat_id = 1000 --起始id
local combat_loading_timeout = 30 --战斗加载超时时间(s)

local function make_combat_id()
    new_combat_id = new_combat_id + 1
    return new_combat_id
end

-------------------------------------------------------------------------------
---! 战斗载入相关处理
-------------------------------------------------------------------------------
local M = {}
COMBAT_D:register_entry("loading", M)

---! 开始战斗加载
function M:start_loading(user_obs)
    local combat_id = make_combat_id()
    local combat_ob = COMBAT_OB:create()
    combat_ob:set_id(combat_id)
    combat_ob:set_map_id(1) ---! 当前有且仅有一张地图

    for _, user_ob in ipairs(user_obs) do
        combat_ob:join_combat(user_ob)
        user_ob:set_combat(combat_ob)
        spdlog.debug("combat", "user_id : %s start loading combat : %s map_id : %s", user_ob:get_id(), combat_ob:get_id(), combat_ob:get_map_id())
    end

    ---! 定时检查超时加载 超时没有响应的客户端,则有多少人完成就多少人进入战斗
    local timer_id = TIMER_D:start_timer(
        combat_loading_timeout,
        1,
        function()
            if combat_ob:get_fighting() then
                return
            end

            local user_ids = combat_ob:get_schedule_not_finish()
            for _, user_id in ipairs(user_ids) do
                local user_ob = USER_D:find_user(user_id)
                if user_ob then
                    spdlog.debug("combat", "user_id : %s loading combat : %s timeout.", user_id, combat_ob:get_id())

                    ---! 退出当前战斗
                    user_ob:quit_combat()

                    ---! 广播其他玩家当前玩家加载超时
                    local result = {}
                    result.user_id = user_id
                    result.timeout = true
                    combat_ob:brocast_packet("tanker.S2CFinishLoading", result)

                    ---! 单独通知指定玩家超时退出游戏
                    local result = {}
                    result.status = 1
                    result.uid = user_id
                    user_ob:send_packet("tanker.S2CQuitCombat", result)

                    ---! 记录玩家退出时间
                    combat_ob:set_temp("quit_time", user_ob:get_id(), os.time())
                end
            end

            --剩余的开始游戏
            COMBAT_D:prepare_combat(combat_ob)
        end
    )
    combat_ob:set_temp("prepare_timer_id", timer_id)
end

---! 完成战斗加载
function M:finish_loading(user_ob, schedule)
    local combat_ob = user_ob:get_combat()
    if not combat_ob then
        spdlog.debug("combat", "C2SFinishLoading user_id:%s combat_ob nil", user_ob:get_id())
        return
    end

    if not combat_ob:query_temp("prepare_timer_id") then
        spdlog.debug("combat", "C2SFinishLoading user_id:%s combat_ob not prepare", user_ob:get_id())
        return
    end

    ---! 更新加载进度条
    combat_ob:set_schedule(user_ob:get_id(), schedule)
    spdlog.debug("combat", "C2SFinishLoading user_id:%s finish loading schedule : %s", user_ob:get_id(), schedule)

    ---! 广播所有玩家更新进度
    local result = {}
    result.user_id = user_ob:get_id()
    result.schedule = schedule
    combat_ob:brocast_packet("tanker.S2CFinishLoading", result)

    ---! 检查所有进度条
    if combat_ob:check_schedule_finish() then
        COMBAT_D:prepare_combat(combat_ob)
    end
end
